package de.tabuma.ssoclient.logik;

import de.tabuma.lib.webservice.client.*;
import java.util.*;
import de.tabuma.ssoclient.entity.Privileg;

/**
 *
 * @author alefen
 */
public class SSOFacade {
    public static String soapUrl = "https://tabumasso.appspot.com/webservice";
    public static String getSSO(String username, String password) throws Exception {
        String methodName = "SSOService_getSSO";
        String soapAction = "SSOService_getSSO";

        Class returnType = String.class;
        Object params[] = {username, password};

        String user = username+"_"+password;

        String obj = SSOHashFacade.get(user); 
        if(obj==null) {
            obj = (String) ExecuteMethod.executeMethod(methodName, soapAction, soapUrl, params, returnType, null);
            SSOHashFacade.put(user, obj);
        }
        return obj;
    }

    public static boolean sessionIsActiv(String ssoId) throws Exception {
        String methodName = "SSOService_sessionIsActiv";
        String soapAction = "SSOService_sessionIsActiv";

        Class returnType = Boolean.class;
        Object params[] = {ssoId};

        Boolean obj = (Boolean) ExecuteMethod.executeMethod(methodName, soapAction, soapUrl, params, returnType, null);
        return obj.booleanValue();
    }

    public static int userIsProjectAdmin(String ssoId, int projectId) throws Exception {
        String methodName = "SSOService_UserIsProjectAdmin";
        String soapAction = "SSOService_UserIsProjectAdmin";

        Class returnType = Integer.class;
        Object params[] = {ssoId, Integer.valueOf(projectId)};

        Integer obj = (Integer) ExecuteMethod.executeMethod(methodName, soapAction, soapUrl, params, returnType, null);
        return obj;
    }

    public static List<Privileg> getUserPrivileges(String ssoId) throws Exception {
        String methodName = "SSOService_getUserPrivileges";
        String soapAction = "SSOService_getUserPrivileges";

        Class returnType = List.class;
        Class returnType1 = Privileg.class;
        Object params[] = {ssoId};

        List<Privileg> obj = SSOHashFacade.getPrivilegList(ssoId);
        if(obj==null) {
            obj = (List<Privileg>) ExecuteMethod.executeMethod(methodName, soapAction, soapUrl, params, returnType, returnType1);
            SSOHashFacade.setPrivilegList(obj, ssoId);
        }
        return obj;
    }
}